package com.yuanhua.sim.business.utils;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.querys.MySqlQuery;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Collections;

/**
 * @author songguoxiang
 * @version 1.0
 * @description: TODO
 * @date 2022/6/29 10:16
 */
@Data
@AllArgsConstructor
public class MybatisPlusGenerator {

    public static void main(String[] args) {
            /*数据库配置*/
            DataSourceConfig dataSourceConfig =
                    new DataSourceConfig
                            .Builder("jdbc:mysql://localhost:3306/netty_im?serverTimezone=CTT&characterEncoding=utf8&useUnicode=true&useSSL=false&autoReconnect=true","root","123456")
                            .dbQuery(new MySqlQuery())
                            .build();


            /*创建一共代码生成器对象*/
            AutoGenerator generator = new AutoGenerator(dataSourceConfig);
            String property = System.getProperty("user.dir");
            /*全局配置*/
            GlobalConfig globalConfig = new GlobalConfig.Builder()
                    .fileOverride()
                    .outputDir(property +"/cloud-appservers/sim-business"+ "/src/main/java")
                    .author("yuanhua")
//                .enableKotlin()
                    .enableSwagger()
                    .dateType(DateType.TIME_PACK)
                    .commentDate("yyyy-MM-dd")
                    .build();

            generator.global(globalConfig);

            /*包配置(PackageConfig)*/

            PackageConfig packageConfig = new PackageConfig.Builder()
                    .parent("com.yuanhua.sim")
                    .moduleName("business")
                    .entity("entity")
                    .service("service")
                    .serviceImpl("service.impl")
                    .mapper("mapper")
                    .xml("mapper.xml")
                    .controller("controller")
                    .other("other")
                    .pathInfo(Collections.singletonMap(OutputFile.mapperXml, "E:\\Git\\Gitee项目仓库\\备战秋招项目\\netty-chat-room\\super-im\\cloud-appservers\\sim-business\\src\\main\\resources\\mapper"))
                    .build();

            generator.packageInfo(packageConfig);


            TemplateConfig templateConfig = new TemplateConfig.Builder()
                    .disable(TemplateType.ENTITY)
                    .entity("/templates/entity.java")
//                    .entity("")
                    .service("/templates/service.java")
//                    .service("")
                    .serviceImpl("/templates/serviceImpl.java")
//                    .serviceImpl("")
                    .mapper("/templates/mapper.java")
                    .mapperXml("/templates/mapper.xml")
                    .controller("")
                    .build();

            generator.template(templateConfig);

            /*策略配置*/
            StrategyConfig strategyConfig = new StrategyConfig.Builder()
                    .enableCapitalMode()
                    .enableSkipView()
                    .disableSqlFilter()
                    // 设置需要生成的表名，如果生成全部，可以使用"all"
                    .addInclude("im_user")
                    .addTablePrefix("im_")
                    .build();


            strategyConfig.entityBuilder()
//                    .superClass(Entity.class)
                    .enableChainModel()
                    .enableLombok()
                    .enableRemoveIsPrefix()
                    .enableTableFieldAnnotation()
                    .enableActiveRecord()
                    .naming(NamingStrategy.underline_to_camel)
                    .columnNaming(NamingStrategy.underline_to_camel)
                    //.addIgnoreColumns("id", "created_user", "created_time", "updated_user", "updated_time")
                    .build();
//
//            strategyConfig.controllerBuilder()
////                .superClass(BaseController.class)
////                .enableHyphenStyle()
//                    .enableRestStyle()
//                    .formatFileName("%sController")
//                    .build();

            strategyConfig.serviceBuilder()
//                .superServiceClass(BaseService.class)
//                .superServiceImplClass(BaseServiceImpl.class)
                    .formatServiceFileName("%sService")
                    .formatServiceImplFileName("%sServiceImpl")
                    .build();

            strategyConfig
                    .mapperBuilder()
                    .superClass(BaseMapper.class)
                    .enableMapperAnnotation()
                    .enableBaseResultMap()
                    .enableBaseColumnList()
                    .formatMapperFileName("%sMapper")
                    .formatXmlFileName("%sMapper")
                    .build();

            generator.strategy(strategyConfig);
            /*执行代码生成器*/
            generator.execute();
        }
}
